package cn.gogpay.duoim.sdk.service.v3;

import cn.gogpay.duoim.sdk.model.IMRespResult;
import cn.gogpay.duoim.sdk.model.v3.base.IMCertificate;
import cn.gogpay.duoim.sdk.model.v3.group.req.*;
import cn.gogpay.duoim.sdk.model.v3.group.resp.CreateGroupResp;
import cn.gogpay.duoim.sdk.model.v3.group.resp.GetGroupsInfoResp;
import cn.gogpay.duoim.sdk.service.IMApiFactory;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.json.JSONUtil;
import okhttp3.Request;

public class IMGroupV3Service {

    /**
     * 创建群
     *
     * @param token 凭据
     * @param req   请求
     * @return 响应
     */
    public IMRespResult<CreateGroupResp> createGroup(IMCertificate token, CreateGroupReq req) {
        String param = JSONUtil.toJsonStr(req);
        Request request = IMApiFactory.buildCertifiedRequest(token, param, "/group/create_group");
        String data = IMApiFactory.callRequest(request);
        TypeReference<IMRespResult<CreateGroupResp>> typeReference = new TypeReference<IMRespResult<CreateGroupResp>>() {
        };
        return JSONUtil.toBean(data, typeReference, false);
    }

    /**
     * 设置群信息
     *
     * @param token 凭据
     * @param req   请求
     * @return 响应
     */
    public IMRespResult<String> setGroupInfo(IMCertificate token, SetGroupInfoReq req) {
        String param = JSONUtil.toJsonStr(req);
        Request request = IMApiFactory.buildCertifiedRequest(token, param, "/group/set_group_info");
        String data = IMApiFactory.callRequest(request);
        TypeReference<IMRespResult<String>> typeReference = new TypeReference<IMRespResult<String>>() {
        };
        return JSONUtil.toBean(data, typeReference, false);
    }

    /**
     * 获取指定群信息
     *
     * @param token 凭据
     * @param req   请求
     * @return 响应
     */
    public IMRespResult<GetGroupsInfoResp> getGroupInfo(IMCertificate token, GetGroupsInfoReq req) {
        String param = JSONUtil.toJsonStr(req);
        Request request = IMApiFactory.buildCertifiedRequest(token, param, "/group/get_groups_info");
        String data = IMApiFactory.callRequest(request);
        TypeReference<IMRespResult<GetGroupsInfoResp>> typeReference = new TypeReference<IMRespResult<GetGroupsInfoResp>>() {
        };
        return JSONUtil.toBean(data, typeReference, false);
    }

    /**
     * 申请加群
     *
     * @param token 凭据
     * @param req   请求
     * @return 响应
     */
    public IMRespResult<String> joinGroup(IMCertificate token, JoinGroupReq req) {
        String param = JSONUtil.toJsonStr(req);
        Request request = IMApiFactory.buildCertifiedRequest(token, param, "/group/join_group");
        String data = IMApiFactory.callRequest(request);
        TypeReference<IMRespResult<String>> typeReference = new TypeReference<IMRespResult<String>>() {
        };
        return JSONUtil.toBean(data, typeReference, false);
    }

    /**
     * 邀请进群
     *
     * @param token 凭据
     * @param req   请求
     * @return 响应
     */
    public IMRespResult<String> inviteUserToGroup(IMCertificate token, InviteUserToGroupReq req) {
        String param = JSONUtil.toJsonStr(req);
        Request request = IMApiFactory.buildCertifiedRequest(token, param, "/group/invite_user_to_group");
        String data = IMApiFactory.callRequest(request);
        TypeReference<IMRespResult<String>> typeReference = new TypeReference<IMRespResult<String>>() {
        };
        return JSONUtil.toBean(data, typeReference, false);
    }

    /**
     * 退出群
     *
     * @param token 凭据
     * @param req   请求
     * @return 响应
     */
    public IMRespResult<String> quitGroup(IMCertificate token, QuitGroupReq req) {
        String param = JSONUtil.toJsonStr(req);
        Request request = IMApiFactory.buildCertifiedRequest(token, param, "/group/quit_group");
        String data = IMApiFactory.callRequest(request);
        TypeReference<IMRespResult<String>> typeReference = new TypeReference<IMRespResult<String>>() {
        };
        return JSONUtil.toBean(data, typeReference, false);
    }

    /**
     * 踢出群
     *
     * @param token 凭据
     * @param req   请求
     * @return 响应
     */
    public IMRespResult<String> kickGroupMember(IMCertificate token, KickGroupMemberReq req) {
        String param = JSONUtil.toJsonStr(req);
        Request request = IMApiFactory.buildCertifiedRequest(token, param, "/group/kick_group");
        String data = IMApiFactory.callRequest(request);
        TypeReference<IMRespResult<String>> typeReference = new TypeReference<IMRespResult<String>>() {
        };
        return JSONUtil.toBean(data, typeReference, false);
    }

    /**
     * 解散群
     *
     * @param token 凭据
     * @param req   请求
     * @return 响应
     */
    public IMRespResult<String> dismissGroup(IMCertificate token, DismissGroupReq req) {
        String param = JSONUtil.toJsonStr(req);
        Request request = IMApiFactory.buildCertifiedRequest(token, param, "/group/dismiss_group");
        String data = IMApiFactory.callRequest(request);
        TypeReference<IMRespResult<String>> typeReference = new TypeReference<IMRespResult<String>>() {
        };
        return JSONUtil.toBean(data, typeReference, false);
    }
}
